
*-------------------------*
*-------------------------*
*Main Figures
*------------------------*
*-------------------------*

*************
* Figure 1
*************


*LOAD DATA FOR FIGURE 1a 
*NB: Figure1b color version was taken from a 2021 report written by Department of Veteran Affairs 
import delimited ".\raw_dta\bud_data.csv", clear

gen cpi_2020 = 100/cpi_ind_2020

set scheme s1mono

* Convert CY values to FY
gen ssdi_fy = ssdi_total[_n-1]*.25 + ssdi_total[_n]*.75
gen ssdi_rec_fy = ssdi_rec[_n-1]*.25 + ssdi_rec[_n]*.75
gen ssi_rec_fy = ssi_total_rec[_n-1]*.25 + ssi_total_rec[_n]*.75
gen ins_total_fy = ins_total[_n-1]*.25 + ins_total[_n]*.75
gen wc_fy = wc_total_benefits_paid[_n-1]*.25 + wc_total_benefits_paid[_n]*.75
gen civ_lf_fy = civ_lab_forc[_n-1]*.25 + civ_lab_forc[_n]*.75
gen wc_cov_jobs_fy = wc_cov_jobs[_n-1]*.25 + wc_cov_jobs[_n]*.75

gen vet = (vadc_vba*cpi_2020)/vet_pop
gen ssi = (ssi_total*cpi_2020)/ins_total_fy
gen ssdi = (ssdi_fy*cpi_2020)/ins_total_fy
gen wc = (wc_fy*cpi_2020)/civ_lf_fy

twoway (line vet fiscal_year, lcolor(blue)) (line ssi fiscal_year, lcolor(red)) (line ssdi fiscal_year, lcolor(green)) (line wc fiscal_year, lcolor(orange)), legend(label(1 "VADC") label(2 "SSI") label(3 "SSDI") label(4 "Workers Comp")) xtitle("Fiscal Year") ytitle("Dollars per Eligible Person (2020 Dollars)")  xlabel(1999(2)2020, labsize(small)) graphregion(color(white)) bgcolor(white) legend(cols(4) region(lstyle(none))) 

graph export ${outputfolder}/Figure1a.pdf, as(pdf) replace
graph export ${outputfolder}/Figure1a.eps, as(eps) replace


*LOAD DATA FOR FIGURES 2-5
use "created_dta/${analysissamp}", clear

*************
* Figure 2
*************

preserve
	
global Xs init_approx_age init_white init_black init_hispanic afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi	

cap drop pred_bal resid_endog resid_endog_adj resid_inst resid_inst_adj

	reghdfe ${NEWENDOG} $Xs, absorb(${FE1}) vce(robust)
	predict pred_bal
	
	reghdfe ${NEWENDOG}, a(${FE1}) vce(robust) resid
	predict resid_endog, resid
	summ ${NEWENDOG} if e(sample)
	g resid_endog_adj = resid_endog + r(mean)
	
	reghdfe ${NEWINST}, a(${FE1}) vce(robust) resid
	predict resid_inst, resid
	summ ${NEWINST} if e(sample)
	g resid_inst_adj = resid_inst + r(mean)

	reghdfe ${NEWENDOG} ${NEWINST}, a(${FE1}) vce(robust) resid
	reghdfe pred_bal ${NEWINST}, a(${FE1}) vce(robust) resid

local bwidth 
local degree degree(1)

 summ resid_inst_adj, det
 local r1 = r(p1) 
 local r2 = r(p99)
  _pctile resid_inst_adj, p(2.5, 97.5)
 local r1 = r(r1) 
 local r2 = r(r2) 

twoway (hist resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), bin(16) color(black) fcolor(white) yaxis(2)) (lpolyci pred_bal resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), `degree' `bwidth'  yaxis(1) lwidth(thin)) (lpolyci resid_endog_adj resid_inst_adj if inrange(resid_inst_adj,`r1',`r2'), ciplot(rline) `degree'  `bwidth' yaxis(1)),  ytitle("Density", axis(2)) ytitle("Months Deployed", axis(1)) xtitle("Peer Months Deployed") legend(off) xscale(range(2.5 10.5)) yscale(range(1 12)) xlabel(3(2)11) ylabel(2(2)12, axis(1)) text(7 9 "Predicted Months Deployed" 9.2 8.25 "Months Deployed", size(vsmall)) ///
text(11 3.5 "1st Stage Coef: 0.9606" 10.65 4.2 "(0.0054)" 10.3 4.1 "N: 157,415" 9.95 3.55 "Dep. Var. Mean:   6.516", size(vsmall)) ///
text(9 3.7 "Pred Dep.: 0.0002" 8.65 4.2 "(0.0004)" 8.3 4.1 "N: 157,415" 7.95 3.55 "Dep. Var. Mean:   6.516", size(vsmall)) ///
yscale(alt) yscale(alt axis(2)) 


graph export ${outputfolder}/Figure2.pdf, as(pdf) replace
graph export ${outputfolder}/Figure2.eps, as(eps) replace
restore

****-------------------------*****
****FIGURES 3 and 4
****-------------------------*****
    
global RUNLIST mths_dep_by_q wia_by_ wia_serplus_by_ combat_death_by_ anyvadc_in_ amtvadc_in_ noncom_death_by_ ndi_despair2_by_
global howfar_graph 32 

foreach dv in $RUNLIST {
	
local start = 2
forvalues x = 2(2)14 {
summ `dv'`x'
if r(mean) < 0.0005 {
local start = `start' + 2 
}
}

global depvar `dv'

preserve
forvalues j = 1(1)$howfar_graph {
		replace  ${depvar}`j' = . if num_qtr_allowed < `j'
}
collapse (mean) ${depvar}*
g n = 1 
reshape long ${depvar} , i(n) j(quarter)
drop if quarter > $howfar_graph
drop n 
g n = _n 
tempfile m 
save `m', replace
restore

preserve
clear 
set obs $howfar_graph
g n = _n 
foreach a in 2 { 
g beta`a' = .
g se`a' = . 
g beta2sls`a' = .
g se2sls`a' = . 
}
tempfile store 
save `store', replace
restore

forvalues x = `start'(2)$howfar_graph {
    
if "`dv'" == "mths_dep_by_q" {
qui reghdfe  ${depvar}`x' ${NEWINST} if  num_qtr_allowed >= `x' , abs($FE1) $SEa 
local beta2sls2 = _b[${NEWINST}]
local se2sls2 = _se[${NEWINST}]
}

if "`dv'" != "mths_dep_by_q" {
qui ivreghdfe  ${depvar}`x' (${NEWENDOG} = ${NEWINST}) if num_qtr_allowed >= `x' , abs($FE1) $SEb $ivregsettings
local beta2sls2 = _b[${NEWENDOG}]
local se2sls2 = _se[${NEWENDOG}]
}

preserve
use `store', replace
local loop 2
foreach a in `loop' { 
replace beta2sls`a' = `beta2sls`a'' if n == `x'
replace se2sls`a' = `se2sls`a'' if n == `x'
}
save `store', replace
restore
}

preserve
use `store', clear 
merge 1:1 n using `m', nogen keep(1 3)
foreach a in `loop' { 
if "`dv'" != "mths_dep_by_q" {
replace beta2sls`a' = beta2sls`a'*${SCALE}
replace se2sls`a' = se2sls`a'*${SCALE}
}
g ub2sls`a' = beta2sls`a' + 1.96*se2sls`a'
g lb2sls`a' = beta2sls`a' - 1.96*se2sls`a'


summ lb2sls`a'
local ymin`a' = min(round(r(min),0.001),0)
summ ub2sls`a'
local ymax`a' = round(r(max),0.001)
local tick`a' = round((`ymax`a''-`ymin`a'')/5,0.0001)

summ ${depvar}
local y2max`a' = round(r(max),0.001)
local y2min`a' = round(r(min),0.001)
local tick2`a' = round((`y2max`a''-`y2min`a'')/5,0.0001)
}

if "`dv'" == "mths_dep_by_q" {
	local ymax2 = 1 
	local ymin2 = 0
	local tick2 = 0.2
}

if inlist("`dv'","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") {
	local ymax2 = .8
	local ymin2 = -.4
	local tick2 = .2
}

if inlist("`dv'","combat_death_by_") {
	local ymax2 = .7
	local ymin2 = 0.0
	local tick2 = .2
}

if inlist("`dv'","amtvadc_in_") {
	local ymax2 = 3000
	local ymin2 = 0
	local tick2 = 600
}

if inlist("`dv'","anyvadc_in_") {
	local ymax2 = 15
	local ymin2 = 0
	local tick2 = 3
}

if inlist("`dv'","amtdisability_in_") {
	local ymax2 = 3500
	local ymin2 = 0
	local tick2 = 700
}

local msymb 
if "`dv'" == "mths_dep_by_q" local msymb msym(D)

if inlist("`dv'","mths_dep_by_q","combat_death_by_","wia_serplus_by_","wia_by_","anyvadc_in_","noncom_death_by_","ndi_despair2_by_")  {
	twoway  (scatter beta2sls2 n, mcolor("155 0 0") `msymb' ) (rcap ub2sls2 lb2sls2 n, lcolor("155 0 0")) (scatter beta2sls2  n if n>= 17, mcolor("255 155 155") `msymb' ) (rcap ub2sls2 lb2sls2  n if n>= 17, lcolor("255 155 155")), yline(0, lpattern(dash) ) xtick(1(1)32) xlabel(4 "1yr" 8 "2yr" 12 "3yr" 16 "4yr" 20 "5yr" 24 "6yr" 28 "7yr" 32 "8yr", angle(45) labsize(medlarge)) xtitle("Time after arrival at BCT", size(medlarge))  legend(off) ylabel(`ymin2'(`tick2')`ymax2', format(%5.1f) axis(1)) ytick(`ymin2'(`tick2')`ymax2', axis(1)) 
}  
if inlist("`dv'","amtvadc_in_")  {
	twoway (scatter beta2sls2 n, mcolor("155 0 0")) (rcap ub2sls2 lb2sls2 n, lcolor("155 0 0"))  (scatter beta2sls2  n if n>= 17, mcolor("255 155 155")) (rcap ub2sls2 lb2sls2  n if n>= 17, lcolor("255 155 155")), yline(0, lpattern(dash) ) xtick(1(1)32) xlabel(4 "1yr" 8 "2yr" 12 "3yr" 16 "4yr" 20 "5yr" 24 "6yr" 28 "7yr" 32 "8yr", angle(45) labsize(medlarge)) xtitle("Time after arrival at BCT", size(medlarge))  legend(off) ylabel(`ymin2'(`tick2')`ymax2', format(%-5.0f) axis(1)) ytick(`ymin2'(`tick2')`ymax2', axis(1)) 
} 

local outputfilename
if "`dv'" == "mths_dep_by_q" local outputfilename 3a
if "`dv'" == "wia_by_" local outputfilename 3b
if "`dv'" == "wia_serplus_by_" local outputfilename 3c
if "`dv'" == "combat_death_by_" local outputfilename 3d
if "`dv'" == "anyvadc_in_" local outputfilename 4a
if "`dv'" == "amtvadc_in_" local outputfilename 4b
if "`dv'" == "noncom_death_by_" local outputfilename 4c
if "`dv'" == "ndi_despair2_by_" local outputfilename 4d

graph export "${outputfolder}/Figure`outputfilename'.pdf", as(pdf) replace
graph export "${outputfolder}/Figure`outputfilename'.eps", as(eps) replace

restore
}

*************
* Figure 5   
*************

	global runlist ndi_death_by_ combat_death_by_ noncom_death_by_ ndi_despair2_by_ ndi_suicide_plus2_by_ ndi_drug2_by_ ndi_mv_acc2_by_ ndi_assault2_by_ ndi_other2_by_

	preserve 
	clear 
	set obs 37
	g n = _n 
	g beta = . 
	g se = . 
	g outcome = ""
	g mean = . 
	g year = . 
	save "created_dta/Table4data", replace 
	restore 
	
	eststo clear
	local n = 0 
	foreach fe in FE1 {
	foreach dv in $runlist {
	foreach x in 8 16 24 32 {
		local n = `n' + 1 
	qui eststo `dv'_`x' : ivreghdfe `dv'`x' (${NEWENDOG} = ${NEWINST}) if ${rest} >= `x' , absorb(${`fe'}) robust $ivregsettings
	local beta =  _b[${NEWENDOG}] * 10 
	local se =  _se[${NEWENDOG}] * 10 
	
	qui ivreghdfe `dv'32 (${NEWENDOG} = ${NEWINST}) if ${rest} >= 32 , absorb(${`fe'}) robust $ivregsettings	
	qui sum `dv'32 if e(sample)
	local mean = r(mean)
	
	if "`dv'" == "ndi_death_by_" local outcomelabel "Death (All Causes)"
	if "`dv'" == "combat_death_by_" local outcomelabel "Combat Death"
	if "`dv'" == "noncom_death_by_" local outcomelabel "Noncombat Death"
	if "`dv'" == "ndi_despair2_by_" local outcomelabel "Death of Despair"
	if "`dv'" == "ndi_suicide_plus2_by_" local outcomelabel "Suicide"
	if "`dv'" == "ndi_drug2_by_" local outcomelabel "Drug- or Alcohol-Rel. Death"
	if "`dv'" == "ndi_mv_acc2_by_" local outcomelabel "Motor Vehicle Death"
	if "`dv'" == "ndi_assault2_by_" local outcomelabel "Assault Death"
	if "`dv'" == "ndi_other2_by_" local outcomelabel "Other Noncombat Death"

	preserve 
	use "created_dta/Table4data", clear 
	replace beta = `beta' if n == `n'
	replace se = `se' if n == `n'
	replace mean =  `mean' if n == `n'
	replace outcome = "`outcomelabel'"  if n == `n'
	replace year = `x'/4  if n == `n'
	save "created_dta/Table4data", replace 
	restore 
	}
	}
	}
	
use "created_dta/Table4data", clear 

gen upper95 = beta + 1.96*se
gen lower95 = beta - 1.96*se

replace outcome = "All-Cause Mortality" if outcome == "Death (All Causes)"  
replace outcome = "Drug or Alcohol-Related Death" if outcome == "Drug- or Alcohol-Rel. Death"  

bys outcome: egen temp = min(n)
egen outcome_n = group(temp) 
drop n temp
sort outcome_n 

format beta %9.1f
gen outcome_2 = outcome_n - 0.15
gen outcome_4 = outcome_n - 0.05
gen outcome_6 = outcome_n + 0.05
gen outcome_8 = outcome_n + 0.15

twoway (scatter beta outcome_2 if year == 2, msymbol(o) mcolor(black) msize(small)) ///
	   (scatter beta outcome_4 if year == 4, msymbol(d) mcolor(red) msize(small)) ///
	   (scatter beta outcome_6 if year == 6, msymbol(s) mcolor(blue) msize(small)) ///
	   (scatter beta outcome_8 if year == 8, msymbol(t) mcolor(green) msize(small)) ///
	   (rcap upper95 lower95 outcome_2 if year == 2,  lcolor(black)) ///
       (rcap upper95 lower95 outcome_4 if year == 4,  lcolor(red)) ///
       (rcap upper95 lower95 outcome_6 if year == 6,  lcolor(blue)) ///
	   (rcap upper95 lower95 outcome_8 if year == 8,  lcolor(green)) ///
	   , legend(on rows(1) order(1 "Two Years" 2 "Four Years" 3 "Six Years" 4 "Eight Years") ///
	   pos(2) ring(0) size(vsmall) region(lcolor(white))) ///
	   graphr(c(white)lc(white)) plotregion(fcolor(white)lc(white))  ///
	   yline(0,lp(-)lc(gs10)) ///
	   ylabel(#6, angle(0) grid glp(dot) glc(black*0.2) labc(black) labs(small)) ///
	   ytitle("2SLS Estimate of 10-Month Deployment Effect", size(small)) xtitle("") ///
	   xlabel(1 `""All-Cause" "Mortality""' ///
			  2 `""Combat" "Death""' ///
			  3 `""Noncombat" "Death""' ///
			  4 `""Death of" "Despair""' ///
			  5 "Suicide" ///
			  6 `""Drug or" "Alcohol-Related" "Death""' ///
			  7 `""Motor Vehicle" "Death""' ///
			  8 `""Assault" "Death""' ///
			  9 `""Other" "Noncombat" Death"',  grid glp(dot) glc(black*0.2) labc(black) labs(vsmall))
			  
graph export "${outputfolder}/Figure5.pdf",replace  	
graph export "${outputfolder}/Figure5.eps", as(eps) replace


*************
* Figure 6
*************
use "created_dta/${wholesamp}", clear

forv i = 1/36{
	gen amtssissdi_in_`i' = amtdisability_in_`i' - amtvadc_in_`i'
}
	
	global Xs init_approx_age init_white init_black init_hispanic female afqs_mi  any_moral_waiver init_afqsc init_afqsc_2 init_afqsc_3 married init_nrdep hsd_ged high_school  any_meps_med_fail_cd any_meps_alcohol_fail  any_meps_marijuana_fail any_meps_cocaine_fail any_meps_alcohol_tst_miss any_meps_marijuana_tst_miss any_meps_cocaine_tst_miss ace_first_asvabcl ace_first_asvabco ace_first_asvabel ace_first_asvabfa ace_first_asvabgm ace_first_asvabgt ace_first_asvabmm ace_first_asvabof ace_first_asvabsc cl_mi co_mi el_mi fa_mi gm_mi gt_mi mm_mi of_mi sc_mi
	

foreach wspc in twoinst_cas {
foreach fe in FE1_as { 
foreach dvx in  wia_by_ noncom_death_by_ amtvadc_in_ amtssissdi_in_ { //
	foreach timehorizon in 8 { 

if "`dvx'" == "wia_by_" local lab "Combat Injury x 100" //  w/in `timehorizon' Years After Arrival"
if "`dvx'" == "noncom_death_by_" local lab "Non-Combat Death x 100" // w/in `timehorizon' Yrs"
if "`dvx'" == "amtvadc_in_" local lab "Amt VADC" // `timehorizon' Years After Arrival"
if "`dvx'" == "amtssissdi_in_" local lab "Amt SSI/SSDI" // `timehorizon' Years After Arrival"


local x = 4*(`timehorizon')

local dv `dvx'

preserve
local yr1 2001
local midyr 2006

if inlist("`dvx'","inc_combined_by_","noncom_death_by_","ndi_despair2_by_","ndi_suicide_plus2_by_") local midyr 2008
if inlist("`dvx'","ndi_war_by_","combat_death_by_","wia_by_") local midyr 2005

local endyr = 2019 - `timehorizon'

if "`wspc'" == "twoinst_cas" noi ivreghdfe `dv'`x' $Xs an_samp_dum (${NEWENDOG} ${NEWENDOG}_casrate  = ${NEWINST} ${NEWINST}_casrate ) if ${rest} >= `x' , absorb(FE = ${`fe'}) robust $ivregsettings

keep if e(sample)



predict pred_X_and_dep 
if "`wspc'" == "IV" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG}
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG}
}

if "`wspc'" == "twoinst_cas" {
g pred_dep = _b[${NEWENDOG}]*${NEWENDOG} + _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
g pred_X_no_dep = pred_X_and_dep -  _b[${NEWENDOG}]*${NEWENDOG} - _b[${NEWENDOG}_casrate]*${NEWENDOG}_casrate
}
g pred_FE = FE

*---------*
*COLLAPSE TO COHORT (i.e. year_arrival)
*---------*

g C = 1
noi summ year_arrival
collapse (mean)   `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  (sum) C, by(year_arrival)

*normalize in 2001 
foreach v in `dv'`x' pred_X_and_dep pred_X_no_dep pred_dep pred_FE  {
summ `v' if year_arrival == `yr1' 
replace `v'= `v'-r(mean)
}

			
*MAKE GRAPH 
local ydec 3
if inlist("`dvx'","amtdisability_in_","amtvadc_in_", "amtssissdi_in_") local ydec 0
if inlist("`dvx'","wia_by_","noncom_death_by_") local ydec 1
 
local deplab = "Dep"
if "`wspc'" == "twoinst_cas" local deplab "Dep. and Peer Cas."
if "`wspc'" == "twoinst_wia" local deplab "Dep and Peer WIA"
if "`wspc'" == "twoinst_kia" local deplab "Dep and Peer KIA"


twoway ///
 (line pred_X_and_dep year_arrival, lcolor("155 0 0") lpattern(--.) lwidth(medthick) yaxis(1)) ///
(line pred_X_no_dep year_arrival, lcolor("185 140 80") lpattern(longdash) lwidth(medthick) yaxis(1)) ///
 (line `dv'`x' year_arrival, lcolor("gs4") lpattern(line) lwidth(medthick) yaxis(1)) ///
 (line pred_dep year_arrival, lcolor("85 140 180") lpattern(-) lwidth(medthick) yaxis(1)) ///\
, yline(0, lcolor(gs3) lwidth(thin)) ytitle("`lab' (Relative to 2001)", axis(1)) ylabel(, format(%9.`ydec'fc)) ///
xtick(`yr1'(1)`endyr') xlabel(`yr1'(1)`endyr') xtitle("Cohort") legend(label(1 "Explained by Xs and `deplab'") label(2 "Explained by Xs") label(3 "Observed Outcome") label(4 "Explained by `deplab'") order(3 2 4 1) symxsize(small) size(small)) name(`dvx'_`wspc'`x', replace)

if "`dvx'" == "wia_by_" local outcomefilename a
if "`dvx'" == "noncom_death_by_" local outcomefilename b
if "`dvx'" == "amtvadc_in_" local outcomefilename c
if "`dvx'" == "amtssissdi_in_" local outcomefilename d

graph export "${outputfolder}/Figure6`outcomefilename'.pdf", as(pdf) replace
graph export "${outputfolder}/Figure6`outcomefilename'.eps", as(eps) replace


restore
	}
}
}
}


************************
* FIGURE 7 *************
*******************************
use "created_dta/${analysissamp}", clear

g ssissdi_amt_in_32 = amtdisability_in_32 - amtvadc_in_32
g ssissdi_amt_in_24 =  amtdisability_in_24 - amtvadc_in_24

egen mths_dep_win_6yr = rowtotal(deployed_0-deployed_71)
egen mths_dep_win_8yr = rowtotal(deployed_0-deployed_95)


g dep_win_6yr = mths_dep_win_6yr > 0 
replace  dep_win_6yr = . if mths_dep_win_6yr== . 

g dep_win_8yr = mths_dep_win_8yr > 0
replace  dep_win_8yr = . if mths_dep_win_8yr== . 

global OUTCOMESET  combat_death_by_ amtvadc_in_ ssissdi_amt_in_ noncom_death_by_


foreach outcome in $OUTCOMESET   {
 

if "`outcome'" != "vantage_v4_score731" {	
g Y0`outcome'32 = -`outcome'32*(1-dep_win_3yr)
g Y0`outcome'24 = -`outcome'24*(1-dep_win_3yr)
}

if "`outcome'" == "vantage_v4_score731" {	
g Y0`outcome' = - `outcome'*(1-dep_win_3yr)
g Y02`outcome' = - `outcome'*(1-dep_win_3yr)
}

preserve 
clear 
set obs 20
g beta = . 
g se = . 
g N = . 
g y0_beta =. 
g sey0 = .
g Ny0 = . 
g rawmean = . 
g rawmean_6yr = . 
g rawmean_8yr = . 
g cohortstart = 2004 + _n
g outcome = ""  
save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
restore 

	foreach cohortset in 2005 2006 2007 2008 2009 2010 2011 {
		local cohortsetplus2 = `cohortset' + 2
if "`outcome'" != "vantage_v4_score731" {			
ivreghdfe `outcome'24 (${NEWENDOG} = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
	summ `outcome'24 if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome'24 if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome'24 if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}
if "`outcome'" == "vantage_v4_score731" {			
ivreghdfe `outcome' (${NEWENDOG} = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
	summ `outcome' if dep_win_3yr == 0 & e(sample)
	local rawmean = r(mean)
	summ `outcome' if dep_win_6yr == 0 & e(sample)
	local rawmean6 = r(mean)
	summ `outcome' if dep_win_8yr == 0 & e(sample)
	local rawmean8 = r(mean)
}


	preserve 
	use "created_dta/rolling_cohorts_6yr_`outcome'", clear 
	replace beta = _b[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace se = _se[${NEWENDOG}]*10 if cohortstart == `cohortset'
	replace N = e(N) if cohortstart == `cohortset'
	replace rawmean = `rawmean' if cohortstart == `cohortset'
	replace rawmean_6yr = `rawmean6' if cohortstart == `cohortset'
	replace rawmean_8yr = `rawmean8' if cohortstart == `cohortset'	
	save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
	restore


if "`outcome'" != "vantage_v4_score731" {	
ivreghdfe Y0`outcome'24 (dep_win_3yr = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
}
if "`outcome'" == "vantage_v4_score731" {	
ivreghdfe Y02`outcome' (dep_win_3yr = ${NEWINST}) if ${rest} >= 24 & inrange(year_arrival,`cohortset',`cohortsetplus2'), absorb(${FE1}) robust $ivregsettings
}
	preserve 
	use "created_dta/rolling_cohorts_6yr_`outcome'", clear 
	replace y0_beta = _b[dep_win_3yr] if cohortstart == `cohortset'
	replace sey0 = _se[dep_win_3yr] if cohortstart == `cohortset'
	replace Ny0 = e(N) if cohortstart == `cohortset'
	save "created_dta/rolling_cohorts_6yr_`outcome'", replace 
	restore
	}
}


 global OUTCOMESET2  combat_death_by_ ssissdi_amt_in_ noncom_death_by_ 


use "created_dta/rolling_cohorts_6yr_amtvadc_in_", clear 
replace outcome = "amtvadc_in_"
foreach outcome in $OUTCOMESET2 {
append using "created_dta/rolling_cohorts_6yr_`outcome'"
replace outcome = "`outcome'" if outcome == ""
}
save "created_dta/rolling_cohorts_6yr_all", replace 

use "created_dta/rolling_cohorts_6yr_all", clear 
g ub = beta + 1.96*se
g lb = beta - 1.96*se

g ub2 = y0_beta + 1.96*sey0
g lb2 = y0_beta - 1.96*sey0

drop if beta == . 

g y10 = y0_beta + beta 
g ub10 = y10 + 1.96*se 
g lb10 = y10 - 1.96*se 

replace y0_beta = 0 if outcome == "combat_death_by_"
replace y10 = beta if outcome == "combat_death_by_"
replace ub10 = y10 + 1.96*se if outcome == "combat_death_by_" 
replace lb10 = y10 - 1.96*se if outcome == "combat_death_by_" 

foreach outcome in combat_death_by_ amtvadc_in_ ssissdi_amt_in_ noncom_death_by_  { 
if "`outcome'" == "either_by_" local ylab "Combat Risk (WIA + Combat Death)"
if "`outcome'" == "either_12_" local ylab "Combat Risk (WIA + Combat Death)"
if "`outcome'" == "amtvadc_in_" local ylab "VADC Amount"
if "`outcome'" == "anyssdissi_in_" local ylab "Any SSI or SSDI Receipt"
if "`outcome'" == "ssissdi_amt_in_" local ylab "SSI or SSDI Amount"
if "`outcome'" == "noncom_death_by_" local ylab "Noncombat Death"
if "`outcome'" == "wia_by_" local ylab "Combat Injury"
if "`outcome'" == "combat_death_by_" local ylab "Combat Death"
if "`outcome'" == "ndi_despair2_by_" local ylab "Death of Despair"
if "`outcome'" == "bar_or_misconduct_by_" local ylab "Bar or Misconduct Separation"
if "`outcome'" == "inc_combined_by_" local ylab "Incarceration"
if "`outcome'" == "vantage_v4_score731" local ylab "Vantage Credit Score"
if "`outcome'" == "nsc_enr_postarr_by_" local ylab "Any Postsecondary Enrollment"
if "`outcome'" == "nsc_asscplus_postarr_by_" local ylab "Any Associate+ Degree"

twoway (scatter y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1)) (line y10 cohortstart if outcome == "`outcome'", mcolor(black) yaxis(1))  (rcap ub10 lb10 cohortstart if outcome == "`outcome'", lcolor(gs7) yaxis(1))  (scatter y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)) (line y0_beta cohortstart if outcome == "`outcome'", mcolor(gs5) yaxis(1)),  legend(order(1 4) label(1 "Deploying Compliers (10 Months Dep.)") label(4 "Nondeploying Compliers")) xtitle("Cohorts") ytitle("`ylab'") ///
xlabel(2005 "05-07" 2006 "06-08" 2007 "07-09" 2008 "08-10" 2009 "09-11" 2010 "10-12" 2011 "11-13"    ) xscale(range(2005 2011)) xtick(2005(1)2011) 


if "`outcome'" == "combat_death_by_" local outcomefilename a
if "`outcome'" == "amtvadc_in_" local outcomefilename b
if "`outcome'" == "ssissdi_amt_in_" local outcomefilename c
if "`outcome'" == "noncom_death_by_" local outcomefilename d

graph export "${outputfolder}/Figure7`outcomefilename'.pdf", replace as(pdf) 
graph export "${outputfolder}/Figure7`outcomefilename'.eps", replace as(eps) 

}
